$(document).ready(function() {
	LoadMap();
});
$("input[name=localorint]").change(function() {
	var a = $("input:radio[name=localorint]:checked").val();
	if (a == 0) {
		$(".property-filter .location").hide()
	} else {
		$(".property-filter .location").show()
	}
	LoadMap();
});
$("input[name=propertyfor]").change(function() {
	LoadMap();
});
$("#inputLocation").change(function() {
	LoadMap();
});
$("#inputType").change(function() {
	LoadMap();
});
$("#bedrooms").change(function() {
	LoadMap();
});
$("#bathrooms").change(function() {
	LoadMap();
});
function GetValueForSearchByMap() {
	url = siteurl + "listingmap/xmlListing?";
	var b = false; //$("input:radio[name=propertyfor]:checked").val();
	//var f = $("input:radio[name=localorint]:checked").val();
	var f = $('input[name=\'localorint\']').attr('value');
	var e = $('select[name="cities"]').val();
	var d = $('select[name="ddlPropertyType"]').val();
	var p = $('select[name="propertytype"]').val();
	var c = $('select[name="bedrooms"]').val();
	var g = $('select[name="bathrooms"]').val();
	if (f) {
		url += "localorint=" + encodeURIComponent(f)
	} 
	if (b) {
		url += "&propertyfor=" + encodeURIComponent(b)
	}
	if (e) {
		url += "&cities=" + encodeURIComponent(e)
	}
	if (p) {
		url += "&propertytype=" + encodeURIComponent(p)
	}
	if (c) {
		url += "&bedrooms=" + encodeURIComponent(c)
	}
	if (g) {
		url += "&bathrooms=" + encodeURIComponent(g)
	}
	return url
}
function getXmlData(a) {
	var b = [];
	$.ajax( {
		url : a,
		dataType : "json",
		async : false,
		beforeSend : function() {
		},
		complete : function() {
		},
		success : function(c) {
			b = c
		},
		error : function(e, c, d) {
			alert(d + "\r\n" + e.statusText + "\r\n" + e.responseText)
		}
	});
	return b
}
function LoadMap() {
	var a = [];
	var e = GetValueForSearchByMap();
	locationsObj = getXmlData(e);
	if (locationsObj.length > 0) {
		for (i = 0; i < locationsObj.length; i++) {
			a.push( {
				0 : locationsObj[i]["latitude"],
				1 : locationsObj[i]["longitude"],
				2 : locationsObj[i]["projectname"],
				3 : locationsObj[i]["streetname"],
				4 : locationsObj[i]["propertyfor"],
				5 : locationsObj[i]["floorarea"],
				6 : locationsObj[i]["askingprice"],
				7 : locationsObj[i]["listing_id"],
				8 : locationsObj[i]["thumbnail"],
				9 : locationsObj[i]["bedroom"],
				10 : locationsObj[i]["bathroom"]
			})
		}
	}
	var h = new Array();
	var g = 0;
	var c = 0;
	var d = 0;
	$.each(a, function(k, j) {
		g = j[0];
		c = j[1];
		return false;
	});
	
	var b = {
		center : new google.maps.LatLng(g, c),
		zoom : 10,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		scrollwheel : true
	};
	var f = new google.maps.Map(document.getElementById("map"), b);
	$.each(a,function(m, l) {
		var k = new google.maps.Marker({
		position : new google.maps.LatLng(l[0],l[1]),
			map : f,
			icon : "http://html.realia.byaviators.com/assets/img/marker-transparent.png"
		});
		var j = {
			content : '<div class="infobox"><div class="image"><img src="'
					+ l[8]
					+ '" alt=""></div><div class="title"><a href="detail/'
					+ l[7]
					+ '">'
					+ l[2]
					+ '</a></div><div class="area"><span class="key">Area:&nbsp;</span><span class="value">'
					+ l[5]
					+ ' sqm</span></div><div class="price">Price: '
					+ number_format(l[6])
					+ ' IDR</div><div class="link"><a href="detail/'
					+ l[7] + '">View more</a></div></div>',
			disableAutoPan : false,
			maxWidth : 0,
			pixelOffset : new google.maps.Size(-146, -190),
			zIndex : null,
			closeBoxURL : "http://drupal.properta.byaviators.com/sites/all/themes/properta/img/close.png",
			infoBoxClearance : new google.maps.Size(1, 1),
			position : new google.maps.LatLng(l[0], l[1]),
			isHidden : false,
			pane : "floatPane",
			enableEventPropagation : false
		};
		k.infobox = new InfoBox(j);
		k.infobox.isOpen = false;
		var j = {
			draggable : true,
			content : '<div class="marker"><div class="marker-inner"></div></div>',
			disableAutoPan : true,
			pixelOffset : new google.maps.Size(-21, -58),
			position : new google.maps.LatLng(l[0], l[1]),
			closeBoxURL : "",
			isHidden : false,
			enableEventPropagation : true
		};
		k.marker = new InfoBox(j);
		k.marker.open(f, k);
		h.push(k);
		google.maps.event.addListener(k, "click", function(o) {
			var n = this;
			$.each(h, function(q, p) {
				if (p !== n) {
					p.infobox.close();
					p.infobox.isOpen = false
				}
			});
			if (n.infobox.isOpen === false) {
				n.infobox.open(f, this);
				n.infobox.isOpen = true;
				f.panTo(n.getPosition())
			} else {
				n.infobox.close();
				n.infobox.isOpen = false
			}
		})
	})
};